<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/2 0002
 * Time: 09:44
 */
namespace app\home\model\order;
use app\home\model\BaseModel;
use app\home\model\dealer\Dealer;
use app\home\model\report\Report;
use app\home\model\report\ReportGoods;
use app\home\model\report\ReportMaxMoney;
use app\home\model\report\ReportTeam;

class GoodsOrder extends BaseModel
{
    protected $autoWriteTimestamp='datetime';

    public $goodsOrder  =[];
    public $setGoodsOrder  =[];
    public $setDealer   =[];
    public $setDealerId =[];//代理id
    public $setReportId =[];//报告id
    public $setUser     =[];
    public $setGoods    =[];
    public $setOrder    =[];
    public $setReport    =[];
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**初始化订单
     * @return $this|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function intOrder(){
        if(empty($this->setReport) && empty($this->setReportId)){
            return $this;
        }
        if(empty($this->setDealer) && empty($this->setDealerId)){
            return $this;
        }
        if(!empty($this->setDealerId) && empty($this->setDealer)){
            $this->setDealer     =Dealer::getDealerInfos($this->setDealerId);//获取到代理信息
        }
        if(!empty($this->setReportId) && empty($this->setReport)){
            $this->setReport     =Report::reportInfo($this->setReportId,$this->setDealer['level']);//获取到报告信息
        }

        return $this;
    }

    /**设置代理信息
     * @return $this
     */
    public function setDealer(){
        if(empty($this->setDealer)){
            return $this;
        }
        if(empty($this->setReportId)){
            return $this;
        }
        $this->goodsOrder['dealer_id']      =$this->setDealer['dealer_id'];
        $this->goodsOrder['report_goods_id']=$this->setGoodsOrder['report_goods_id'];//代理设置的报告产品id
        $this->goodsOrder['f_dealer_id']    =$this->setDealer['f_dealer_id'];//上级代理id
        $this->goodsOrder['f_get_money']    =(new ReportTeam())->reportTeamMoney($this->setDealer['f_dealer_id'],$this->setReportId);
        //上级代理获得金额（成本差）

        return $this;
    }

    /**设置用户信息
     * @return $this
     */
    public function setUser(){
        if(empty($this->setUser)){
            return $this;
        }
        $this->goodsOrder['user_name'] =$this->setUser['user_name'];
        $this->goodsOrder['user_phone']=num_secret($this->setUser['user_phone']);//电话加密
        $this->goodsOrder['user_idnum']=num_secret($this->setUser['user_idnum']);//身份证加密
        return $this;
    }

    /**设置商品信息
     * @return $this
     */
    public function setGoods(){
        $this->goodsOrder['report_code']=$this->setReport['report_code'];//报告唯一码
        $this->goodsOrder['report_name']=$this->setReport['name'];//报告唯一码
        return $this;
    }

    /**订单设置
     * @param $dealerId
     * @param $reportId
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setOrder(){
//        $ReportGoods      =ReportGoods::reportGoods($dealerId,$reportId);
        $pay_money        =$this->setGoodsOrder['report_pay'];
        $this->goodsOrder['pay_money']  =$pay_money;//用户支付价
        $Team =new ReportTeam();
        $MaxMoney= new ReportMaxMoney();
        $cost_money= $Team->costMoney($this->setDealerId,$this->setReportId);
        $this->goodsOrder['cost_money'] =$cost_money;//成本价，动态计算出
        //溢价手续费
        $sermoney=$MaxMoney->serviceCharge($pay_money,$this->setReport['free_money'],$this->setReport['reportMaxMoney']);//溢价手续费

        $this->goodsOrder['get_money']  =$pay_money-$this->goodsOrder['cost_money']-$sermoney;//该代理得到金额=佣金-手续服务费
        if($this->goodsOrder['get_money'] < 0){
            $this->goodsOrder['get_money'] =0;
        }
        $this->goodsOrder['order_code']    =getCodeOrder(14,$this->setReport['report_code']);
        $this->goodsOrder['create_date']   =date("Y-m-d",time());
        $this->goodsOrder['service_money'] =$sermoney;//溢价手续费

    }

    /**创建订单
     * @param int $reportId
     * @param int $dealerId
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createOrder($reportId=0,$dealerId=0){
        if(!empty($reportId)){
            $this->setReportId=$reportId;
        }
        if(!empty($dealerId)){
            $this->setDealerId=$dealerId;
        }
        if(empty($reportId) && empty( $this->setReportId)){
            return false;
        }
        if(empty($dealerId) && empty( $this->setDealerId)){
            return false;
        }
        $this->intOrder()
            ->setDealer()
            ->setUser()
            ->setGoods()
            ->setOrder();
//        print_r($this->goodsOrder);die;
        if(empty($this->goodsOrder)){
            return false;
        }
        $rel= $this->save($this->goodsOrder);
        setStcOrder('order_blanket',$this->setReport['report_code']);//统计订单 总订单数量
        msgOrderSee($this->setDealerId,0,0);
        msgOrderSee($this->setDealerId,0,3);
        return $rel;
    }

    /**获取到订单编号
     * @return mixed|string
     */
    public function getOrderCode(){
        if(!empty($this->goodsOrder['order_code'])){
            return $this->goodsOrder['order_code'];
        }else{
            return '';
        }
    }

    /**用户手机号
     * @param $phone
     */
    public function getUserPhoneAttr($phone){
        if(preg_match("/^\d*$/",substr($phone,0,1))){
            return $phone;
        }else{
            return num_secret($phone);
        }
    }

    /**用户身份证号
     * @param $idNum
     */
    public function getUserIdnumAttr($idNum){
        if(preg_match("/^\d*$/",substr($idNum,0,1))){
            return $idNum;
        }else{
            return num_secret($idNum);
        }
    }
}